package com.spring.framework.config.autowired.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * Java泛型类型也可以隐式作为类型选择，可以用在属性或者参数上。如果某一些bean都实现了一个通用的接口，那么可以使用接口类型和泛型类型作
 * 为属性的类型，这同样适用于数组和集合。
 *
 * @author xuweizhi
 * @since 2020/12/15 14:25
 */
@Component
public class AutowiredGenericDemo {

	@Autowired
	private AutowiredGeneric<AutowiredGenericA> autowiredGenericAAutowiredGeneric;

	@Autowired
	private AutowiredGeneric<AutowiredGenericB> autowiredGenericBAutowiredGeneric;

	@Autowired
	private AutowiredGeneric<AutowiredGenericC> autowiredGenericCAutowiredGeneric;

	@Autowired
	private AutowiredGeneric<AutowiredGenericA>[] autowiredGenerics;

	//……其他集合也可以

	@Component
	public static class AutowiredGenericA implements AutowiredGeneric<AutowiredGenericA> {

	}

	@Component
	public static class AutowiredGenericB implements AutowiredGeneric<AutowiredGenericB> {
	}

	@Component
	public static class AutowiredGenericC implements AutowiredGeneric<AutowiredGenericC> {
	}

	public interface AutowiredGeneric<T> {

	}

	@Override
	public String toString() {
		return "AutowiredGenericDemo{" + "\n" +
				"autowiredGenericAAutowiredGeneric=" + autowiredGenericAAutowiredGeneric + "\n" +
				", autowiredGenericBAutowiredGeneric=" + autowiredGenericBAutowiredGeneric + "\n" +
				", autowiredGenericCAutowiredGeneric=" + autowiredGenericCAutowiredGeneric + "\n" +
				", autowiredGenerics=" + Arrays.toString(autowiredGenerics) +
				'}';
	}
}
